using System;
using System.ComponentModel;
using Microsoft.Practices.WizardFramework;
using VSSDKAssist.Assets.Converters;
using VSSDKAssist.Recipes.Common.CustomPages;
using VSSDKAssist.Recipes.Common.Helpers;

namespace VSSDKAssist.Recipes.CreateToolOptionsPage
{
	/// <summary>
	/// Custom page for CreateToolOptionsPageCP3 recipe
	/// </summary>
	public partial class CreateToolOptionsPageCP3 : WizardPageBase
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="CreateToolOptionsPageCP3"/> class.
		/// </summary>
		public CreateToolOptionsPageCP3()
		{
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="CreateToolOptionsPageCP3"/> class.
		/// </summary>
		/// <param name="wizard">The <see cref="T:Microsoft.Practices.WizardFramework.WizardForm"/>that will host the new page.</param>
		public CreateToolOptionsPageCP3(WizardForm wizard)
            : base(wizard)
        {
            InitializeComponent();
			SetControls();
        }

		/// <summary>
		/// Gets or sets the name of the option page.
		/// </summary>
		/// <value>The name of the option page.</value>
		[RecipeArgument]
		public string OptionPageName
		{
			get { return veOptionPageName.Value.ToString(); }
			set
			{
				if(value != null)
				{
					veOptionPageName.TextBox.Text = value;
				}
			}
		}

		/// <summary>
		/// Gets or sets the name of the option page control.
		/// </summary>
		/// <value>The name of the option page control.</value>
		[RecipeArgument]
		public string OptionPageControlName
		{
			get { return veOptionPageControlName.Value.ToString(); }
			set
			{
				if(value != null)
				{
					veOptionPageControlName.Text = value;
				}
			}
		}

		/// <summary>
		/// Gets or sets a value indicating whether [supports automation].
		/// </summary>
		/// <value><c>true</c> if [supports automation]; otherwise, <c>false</c>.</value>
		[RecipeArgument]
		public bool SupportsAutomation
		{
			get { return chkSupportAutomation.Checked; }
			set
			{
				chkSupportAutomation.Checked = value;
			}
		}

		/// <summary>
		/// Gets or sets a value indicating whether this instance is custom page.
		/// </summary>
		/// <value>
		/// 	<c>true</c> if this instance is custom page; otherwise, <c>false</c>.
		/// </value>
		[RecipeArgument]
		public bool IsCustomPage
		{
			get { return chkIsCustomPage.Checked; }
			set
			{
				chkIsCustomPage.Checked = value;
			}
		}

		/// <summary>
		/// 	<see cref="P:Microsoft.WizardFramework.WizardPage.IsDataValid"/>
		/// Search for all the arguments that this Wizard gather
		/// If all of them are not null, then the Wizard can finish
		/// </summary>
		/// <value></value>
		public override bool IsDataValid
		{
			get
			{
				errorProvider.SetError(veOptionPageName, string.Empty);
				errorProvider.SetError(veOptionPageControlName, string.Empty);

				if(veOptionPageName.Value != null)
				{
					if(CustomPageHelper.ProjectItemExists(
						DictionaryService,
						"VSPackageProject",
						"DestinationFolder",
                        string.Concat(veOptionPageName.Value.ToString(), ".", DictionaryService.GetValue("LanguageExtension").ToString())))
					{
						return false;
					}
				}

				if(chkIsCustomPage.Checked)
				{
					if(veOptionPageControlName.Value == null)
					{
						errorProvider.SetError(veOptionPageControlName, Properties.Resources.InvalidValue);
						return false;
					}

					if(veOptionPageName.Value != null && veOptionPageControlName.Value != null)
					{
						if(veOptionPageName.Value.ToString().Equals(
							veOptionPageControlName.Value.ToString(), StringComparison.OrdinalIgnoreCase))
						{
							errorProvider.SetError(veOptionPageName, Properties.Resources.NamesShouldBeDifferent);
							errorProvider.SetError(veOptionPageControlName, Properties.Resources.NamesShouldBeDifferent);

							return false;
						}

						if(CustomPageHelper.ProjectItemExists(
							DictionaryService,
							"VSPackageProject",
							"DestinationFolder",
                            string.Concat(veOptionPageControlName.Value.ToString(), ".", DictionaryService.GetValue("LanguageExtension").ToString())))
						{
							return false;
						}
					}
				}

				return base.IsDataValid;
			}
		}

		/// <summary>
		/// Gets or sets the site of the control.
		/// </summary>
		/// <value></value>
		/// <returns>The <see cref="T:System.ComponentModel.ISite"></see> associated with the <see cref="T:System.Windows.Forms.Control"></see>, if any.</returns>
		/// <PermissionSet><IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/><IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/><IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence"/><IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/></PermissionSet>
		public override ISite Site
		{
			get
			{
				return base.Site;
			}
			set
			{
				base.Site = value;
				if(value != null)
				{
					this.veOptionPageName.BeginInit();
					this.veOptionPageName.ValueType = typeof(string);
					base.Site.Container.Add(veOptionPageName);
					this.veOptionPageName.ConverterInstance = new CSharpIdentifierConverter();
					this.veOptionPageName.EndInit();

					this.veOptionPageControlName.BeginInit();
					this.veOptionPageControlName.ValueType = typeof(string);
					base.Site.Container.Add(veOptionPageControlName);
					this.veOptionPageControlName.ConverterInstance = new CSharpIdentifierConverter();
					this.veOptionPageControlName.EndInit();
				}
			}
		}

		private void veOptionPageName_ValueChanged(object sender, System.ComponentModel.Design.ComponentChangedEventArgs e)
		{
			if(veOptionPageName.ConverterInstance != null)
			{
				errorProvider.SetError(veOptionPageName, string.Empty);

				if(veOptionPageName.Value != null)
				{
					if(CustomPageHelper.ProjectItemExists(
						DictionaryService,
						"VSPackageProject",
						"DestinationFolder",
                        string.Concat(veOptionPageName.Value.ToString(), ".", DictionaryService.GetValue("LanguageExtension").ToString())))
					{
						DictionaryService.SetValue("OptionPageName", null);
						errorProvider.SetError(veOptionPageName, Properties.Resources.InvalidValue);
					}
					else
					{
						DictionaryService.SetValue("OptionPageName", veOptionPageName.Value.ToString());
					}
				}
				else
				{
					DictionaryService.SetValue("OptionPageName", null);
					errorProvider.SetError(veOptionPageName, Properties.Resources.InvalidValue);
				}
			}
			else
			{
				DictionaryService.SetValue("OptionPageName", veOptionPageName.Value.ToString());
			}
		}

		private void veOptionPageControlName_ValueChanged(object sender, System.ComponentModel.Design.ComponentChangedEventArgs e)
		{
			if(veOptionPageControlName.ConverterInstance != null)
			{
				errorProvider.SetError(veOptionPageControlName, string.Empty);

				if(veOptionPageControlName.Value != null)
				{
					if(CustomPageHelper.ProjectItemExists(
						DictionaryService,
						"VSPackageProject",
						"DestinationFolder",
                        string.Concat(veOptionPageControlName.Value.ToString(), ".", DictionaryService.GetValue("LanguageExtension").ToString())))
					{
						DictionaryService.SetValue("OptionPageControlName", null);
						errorProvider.SetError(veOptionPageControlName, Properties.Resources.InvalidValue);
					}
					else
					{
						DictionaryService.SetValue("OptionPageControlName", veOptionPageControlName.Value.ToString());
					}
				}
				else
				{
					DictionaryService.SetValue("OptionPageControlName", null);
					errorProvider.SetError(veOptionPageControlName, Properties.Resources.InvalidValue);
				}
			}
			else
			{
				DictionaryService.SetValue("OptionPageControlName", veOptionPageControlName.Value.ToString());
			}
		}

		private void chkSupportAutomation_CheckedChanged(object sender, EventArgs e)
		{
			DictionaryService.SetValue("SupportsAutomation", chkSupportAutomation.Checked);
		}

		private void chkIsCustomPage_CheckedChanged(object sender, EventArgs e)
		{
			lblOptionPageControlName.Visible = chkIsCustomPage.Checked;
			veOptionPageControlName.Visible = chkIsCustomPage.Checked;
			DictionaryService.SetValue("IsCustomPage", chkIsCustomPage.Checked);
		}

		private void SetControls()
		{
			lblOptionPageControlName.Visible = false;
			veOptionPageControlName.Visible = false;
		}
	}
}